//
//  YSJShareView.swift
//  YSJSharePopView_Swift
//
//  Created by 闫树军 on 16/4/14.
//  Copyright © 2016年 闫树军. All rights reserved.
//  分享视图

import UIKit

class ShareBtn: UIButton {
    var iconImageView : UIImageView!
    var nameLabel     : UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        iconImageView = UIImageView.init(frame: CGRect(x: 10, y: 10, width: 40, height: 40))
        iconImageView.layer.cornerRadius = 5
        imageView?.contentMode = .scaleAspectFill
        self.addSubview(iconImageView)
        
        
        
        nameLabel = UILabel.init(frame: CGRect(x: 0, y: 55, width: 60, height: 20))
        nameLabel.textAlignment = NSTextAlignment.center
        nameLabel.font = UIFont.systemFont(ofSize: 11)
        self.addSubview(nameLabel)
    }
    
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        return CGRect.init(x: 0, y: 0, width: self.frame.width, height: self.frame.width)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

protocol YSJShareViewDelegate {
    func shareBtnClick(_ index:Int)
}


class YSJShareView: UIView {
    
    var shareViewHeight     :CGFloat!
    var _shareView          :UIView!
    var _sepWidth           :CGFloat!
    var _count              :Int = 0
    var _shareViewBackground :UIView!
    var _window             :UIWindow!
    var _shareVieweight     :CGFloat = 120
    var _delegate            :YSJShareViewDelegate!
    
    
    //中间的文字
    lazy var centerLabel: UILabel = {
        let l : UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 5, width: UIScreen.main.bounds.width, height: 20))
        l.font = UIFont.systemFont(ofSize: 14)
        l.text = "分享至"
        l.textAlignment = .center
        return l
    }()
    
    //横线
    lazy var centerLine: UIView = {
        let c : UIView = UIView.init(frame: CGRect.init(x: 10, y: 30, width: UIScreen.main.bounds.width - 20, height: 1))
        c.backgroundColor = UIColor.gray
        return c
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        _shareView = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: _shareVieweight))
        self.addSubview(_shareView)
        
        _shareViewBackground = UIView.init(frame: UIScreen.main.bounds)
        _shareViewBackground.backgroundColor = UIColor.clear
        _shareViewBackground.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(YSJShareView.dismiss)))
        
        //中间文字
        _shareView.addSubview(centerLabel)
        
        //横线
        _shareView.addSubview(centerLine)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addItem(_ title:String ,withImage:UIImage) {
        print(title)
        _count += 1
        _sepWidth = 20 + ((UIScreen.main.bounds.width - 280)/3 + 60)*(CGFloat(_count - 1))
        let shareBtn = ShareBtn.init(type: UIButtonType.custom)
        shareBtn.frame = CGRect(x: 0 + _sepWidth, y: 35, width: 60, height: 80)
        shareBtn.nameLabel.text = title
        //        shareBtn.backgroundColor = UIColor.randomColor()
        shareBtn.iconImageView.image = withImage
        shareBtn.addTarget(self, action: #selector(YSJShareView.share(_:)), for: UIControlEvents.touchUpInside)
        shareBtn.tag = 1000+_count
        if _count > 4{
            _sepWidth = 20 + ((UIScreen.main.bounds.width - 280)/3 + 60)*(CGFloat(_count - 5))
            shareBtn.frame = CGRect(x: 0 + _sepWidth, y: 90, width: 60, height: 80)
        }
        
        _shareView.addSubview(shareBtn)
    }
    
    
    /// 显示分享视图
    func show() {
        _window = UIWindow.init(frame: UIScreen.main.bounds)
        _window.windowLevel = UIWindowLevelAlert+1
        _window.backgroundColor = UIColor.clear
        _window.isHidden = true
        _window.isUserInteractionEnabled = true
        _window.addSubview(_shareViewBackground)
        _window.addSubview(self)
        
        _window.isHidden = false
        UIView.animate(withDuration: 0.2, animations: {
            self._shareViewBackground.backgroundColor = UIColor.init(white: 0.0, alpha: 0.5)
            self.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - self._shareVieweight, width: UIScreen.main.bounds.width, height: self._shareVieweight)
        })
    }
    
    
    /// 隐藏视图
    func hidden() {
        UIView.animate(withDuration: 0.2, animations: {
            self._shareViewBackground.backgroundColor = UIColor.init(white: 0.0, alpha: 0.0)
            self.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height , width: UIScreen.main.bounds.width, height: self._shareVieweight)
        }, completion: { (finished) in
            self._window = nil
        })
        
    }
    
    
    /// 隐藏方法
    func dismiss() {
        hidden()
    }
    
    
    /// 分享类型
    func share(_ btn:UIButton) {
        _delegate.shareBtnClick(btn.tag - 1001)
        hidden()
    }
}


extension UIColor {
    /**
     快速构建rgb颜色
     
     - parameter r: r
     - parameter g: g
     - parameter b: b
     
     - returns: 返回rgb颜色对象，alpha默认1
     */
    class func colorWithRGB(_ r: CGFloat, g: CGFloat, b: CGFloat) -> UIColor {
        return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    }
    
    /**
     生成随机颜色
     
     - returns: 返回随机色
     */
    class func randomColor() -> UIColor {
        let r = CGFloat(arc4random_uniform(256))
        let g = CGFloat(arc4random_uniform(256))
        let b = CGFloat(arc4random_uniform(256))
        return UIColor.colorWithRGB(r, g: g, b: b)
    }
    
}
